﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.Utils;

namespace ActivitiesControl.Domain.Services.Impls
{
	[ExceptionMessageHandler]
	internal class ResourceUserServices
		: BaseServices, IResourceServices, IUserServices
	{
		private UserHandler handler;
		
		public override event MessageHandler MessageRaised = delegate(object sender, MessageEventArgs e) { };

		public ResourceUserServices()
		{
			handler = new UserHandler(UserRepository);
		}

		internal override void RaiseMessage(object sender, MessageEventArgs e)
		{
			MessageRaised(sender, e);
		}

		public IEnumerable<Resource> GetResources()
		{
			throw new NotImplementedException();
		}

		public Resource GetResourceById(int resourceId)
		{
			return ResourceRepository.GetById(resourceId);
		}

		public User GetUserByLogin(string login)
		{
			return UserRepository.GetUserByLogin(login);
		}

		public void UpdateUserPassword(string login, string password, string newPassword, string newPasswordConfirmation)
		{
			handler.ChangePassword(login, password, newPassword, newPasswordConfirmation);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Alteração de senha", "Senha alterada com sucesso.", MessageType.Ok) });
		}

		public bool ValidateUser(string login, string password, out User loggedUser)
		{
			bool validated = handler.Validate(login, password, out loggedUser);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Login", "Usuário logado.", MessageType.Ok) });
			return validated;
		}
	}
}
